package com.itheima.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.config.SpringConfig;
import com.itheima.dao.ContactDao;
import com.itheima.pojo.Contact;
import com.itheima.service.ContactService;
import com.itheima.utils.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;
@Service
public class ContactServiceImpl implements ContactService {
   @Autowired
    private ContactDao contactDao;

    @Override
    public String findAll() {
        Jedis jedis = JedisUtils.getJedis();
        String contacts = jedis.get("contacts");
        if (contacts == null) {
            List<Contact> contactList = contactDao.findAll();
            try {
                contacts = new ObjectMapper().writeValueAsString(contactList);
                jedis.set("contacts", contacts);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return contacts;
    }
}
